package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.DictUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

/**
 * 发布整车Controller
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tRentproduct")
public class TRentproductController extends BaseController {

	@Autowired
	private TRentproductService tRentproductService;
	@Autowired
	private TCargotypeService tCargotypeService;
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private TLineService tLineService;
	
	/**
	 * 整车列表
	 * urlParam的取值如下：
	 * rentproductForDay 按天包车
	 * rentproductForTravel 旅游包车
	 * rentproductForAirport 接送飞机
	 * rentproductForTrain 接送火车
	 */
	@RequiresPermissions("rentproduct:tRentproduct:view")
	@RequestMapping("{urlParam}List")
	public String rentproductList(@PathVariable String urlParam, TRentproduct tRentproduct, Model model, HttpServletRequest request, HttpServletResponse response) {
		// 按天包车
		if(urlParam.equals("rentproductForDay")) {
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_DAY);
		}
		// 旅游包车
		if(urlParam.equals("rentproductForTravel")) {
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_TRAVEL);
		}
		// 接送飞机
		if(urlParam.equals("rentproductForAirport")) {
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_AIRPLANE);
		}
		// 接送火车
		if(urlParam.equals("rentproductForTrain")) {
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_TRAIN);
		}
		
		Page<TRentproduct> page = tRentproductService.findPage(new Page<TRentproduct>(request, response), tRentproduct); 
		model.addAttribute("page", page);
		model.addAttribute("tRentproduct", tRentproduct);
		// 站点数据
		TSitedtl site = new TSitedtl();
		model.addAttribute("siteList", tSitedtlService.findList(site));
		return MessageFormat.format("modules/rentproduct/{0}List", urlParam);
	}
	
	@RequiresPermissions("rentproduct:tRentproduct:view")
	@RequestMapping(value = "{urlParam}Form")
	public String form(@PathVariable String urlParam, TRentproduct tRentproduct, Model model) {
		if (tRentproduct.getRentid() != null){
			tRentproduct = tRentproductService.get(tRentproduct.getRentid().toString());
		} else {
			
			// 单选框默认选中设置
			
			if(ValidateTool.isEmptyOrNull(tRentproduct.getPayway())){
				tRentproduct.setPayway(DictUtils.getDictValue("线上结算", "T_PAY", ""));
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getIsroadtoll())){
				tRentproduct.setIsroadtoll("0");
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getIsparkfee())){
				tRentproduct.setIsparkfee("0");
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getIselectric())){
				tRentproduct.setIselectric("0");
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getIsall())){
				tRentproduct.setIsall("1");
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getIspublic())){
				tRentproduct.setIspublic("1");
			}
			if(ValidateTool.isEmptyOrNull(tRentproduct.getFixedprice())){
				tRentproduct.setFixedprice("1");
			}
		}
		
		// 如果起止时间都为空，则设置默认起止时间
		
		if(tRentproduct.getFromdate() == null && tRentproduct.getStopdate() == null) {
			
			// 租赁开始日期默认第二天及结束日期（默认一年）
			
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			tRentproduct.setFromdate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			tRentproduct.setStopdate(calendar.getTime());
		}
		
		// 按天包车
		if(urlParam.equals("rentproductForDay")) {
			// 站点数据
			TSitedtl site = new TSitedtl();
			model.addAttribute("siteList", tSitedtlService.findList(site));
		}
		// 旅游包车、接送飞机、接送火车
		if(urlParam.equals("rentproductForTravel") ||
				urlParam.equals("rentproductForAirport") ||
				urlParam.equals("rentproductForTrain")) {
			// 线路数据
			TLine line = new TLine();
			line.setLinestatus(TLine.LINESTATUS_VALID);
			List<TLine> lineList = tLineService.findList(line);
			model.addAttribute("lineList", lineList);
		}
		// 车型数据
		List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
		model.addAttribute("cargotypeList", cargotypeList);
		
		model.addAttribute("tRentproduct", tRentproduct);
		
		// 旅行商和电商数据
		List<Office> officeList = tRentproductService.findOfficeList();
		model.addAttribute("officeList", officeList);
		return MessageFormat.format("modules/rentproduct/{0}Form", urlParam);
	}
	
	@RequiresPermissions("rentproduct:tRentproduct:edit")
	@RequestMapping(value = "{urlParam}Save")
	public String save(@PathVariable String urlParam,TRentproduct tRentproduct, Model model, RedirectAttributes redirectAttributes) {
		// 按天包车
		if(urlParam.equals("rentproductForDay")) {
			tRentproduct.setFixedprice("0");
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_DAY);
		}
		// 旅游包车
		if(urlParam.equals("rentproductForTravel")) {
			tRentproduct.setFixedprice("1");
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_TRAVEL);
		}
		// 接送飞机
		if(urlParam.equals("rentproductForAirport")) {
			tRentproduct.setSuitrtnprice(0D);
			tRentproduct.setFixedprice("1");
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_AIRPLANE);
		}
		// 接送火车
		if(urlParam.equals("rentproductForTrain")) {
			tRentproduct.setSuitrtnprice(0D);
			tRentproduct.setFixedprice("1");
			tRentproduct.setRentType(TOrders.ORDER_TYPE_RENTPRODUCT_TRAIN);
		}
		
		// 校验
		
		if (!beanValidator(model, tRentproduct)){
			return form(urlParam, tRentproduct, model);
		}
		tRentproductService.save(tRentproduct);
		addMessage(redirectAttributes, "保存发布整车成功");
		return MessageFormat.format("redirect:/a/rentproduct/tRentproduct/{0}List?repage", urlParam);
	}
	
	@RequiresPermissions("rentproduct:tRentproduct:edit")
	@RequestMapping(value = "{urlParam}Delete")
	public String delete(@PathVariable String urlParam, TRentproduct tRentproduct, RedirectAttributes redirectAttributes) {
		tRentproductService.delete(tRentproduct);
		addMessage(redirectAttributes, "删除发布整车成功");
		return MessageFormat.format("redirect:/a/rentproduct/tRentproduct/{0}List?repage", urlParam);
	}
	
}